﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALStockOrderGoods
    /// </summary>
    public partial class DALStockOrderGoods
    {
        public DALStockOrderGoods()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbStockOrderGoods");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbStockOrderGoods model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbStockOrderGoods(");
            strSql.Append("stockOrderCode,goodsId,goodsCount,modelTypeId,goodsStockPrice)");
            strSql.Append(" values (");
            strSql.Append("@stockOrderCode,@goodsId,@goodsCount,@modelTypeId,@goodsStockPrice)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@stockOrderCode", SqlDbType.VarChar,50),
					new SqlParameter("@goodsId", SqlDbType.Int,4),
					new SqlParameter("@goodsCount", SqlDbType.Int,4),
					new SqlParameter("@modelTypeId", SqlDbType.Int,4),
					new SqlParameter("@goodsStockPrice", SqlDbType.Decimal,9)};
            parameters[0].Value = model.stockOrderCode;
            parameters[1].Value = model.goodsId;
            parameters[2].Value = model.goodsCount;
            parameters[3].Value = model.modelTypeId;
            parameters[4].Value = model.goodsStockPrice;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int AddAndUpdate(Gift.Model.tbStockOrderGoods model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" if exists(select 1 from tbStockOrderGoods where goodsStockPrice = @stockOrderCode and goodsId = @goodsId and modelTypeId = @modelTypeId ) ");
            strSql.Append(" begin ");
            strSql.Append(" update tbStockOrderGoods set stockOrderCode=@stockOrderCode, goodsCount = @goodsCount,goodsStockPrice=@goodsStockPrice  ");
            strSql.Append(" where goodsStockPrice = @stockOrderCode and goodsId = @goodsId and modelTypeId = @modelTypeId  ");
            strSql.Append(" end ");
            strSql.Append(" else ");
            strSql.Append(" begin ");
            strSql.Append("insert into tbStockOrderGoods(");
            strSql.Append("stockOrderCode,goodsId,goodsCount,modelTypeId,goodsStockPrice)");
            strSql.Append(" values (");
            strSql.Append("@stockOrderCode,@goodsId,@goodsCount,@modelTypeId,@goodsStockPrice)");
            strSql.Append(";select @@IDENTITY");
            strSql.Append(" end ");
            SqlParameter[] parameters = {
					new SqlParameter("@stockOrderCode", SqlDbType.VarChar,50),
					new SqlParameter("@goodsId", SqlDbType.Int,4),
					new SqlParameter("@goodsCount", SqlDbType.Int,4),
					new SqlParameter("@modelTypeId", SqlDbType.Int,4),
					new SqlParameter("@goodsStockPrice", SqlDbType.Decimal,9)};
            parameters[0].Value = model.stockOrderCode;
            parameters[1].Value = model.goodsId;
            parameters[2].Value = model.goodsCount;
            parameters[3].Value = model.modelTypeId;
            parameters[4].Value = model.goodsStockPrice;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbStockOrderGoods model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbStockOrderGoods set ");
            strSql.Append("stockOrderCode=@stockOrderCode,");
            strSql.Append("goodsId=@goodsId,");
            strSql.Append("goodsCount=@goodsCount,");
            strSql.Append("modelTypeId=@modelTypeId,");
            strSql.Append("goodsStockPrice=@goodsStockPrice,");
            strSql.Append("inWarehouseCount=@inWarehouseCount");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@stockOrderCode", SqlDbType.VarChar,50),
					new SqlParameter("@goodsId", SqlDbType.Int,4),
					new SqlParameter("@goodsCount", SqlDbType.Int,4),
					new SqlParameter("@modelTypeId", SqlDbType.Int,4),
					new SqlParameter("@goodsStockPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Id", SqlDbType.Int,4),
                    new SqlParameter("@inWarehouseCount", SqlDbType.Int,4)                   
                                        };
            parameters[0].Value = model.stockOrderCode;
            parameters[1].Value = model.goodsId;
            parameters[2].Value = model.goodsCount;
            parameters[3].Value = model.modelTypeId;
            parameters[4].Value = model.goodsStockPrice;
            parameters[5].Value = model.Id;
            parameters[6].Value = model.InWarehouseCount;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbStockOrderGoods ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbStockOrderGoods ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbStockOrderGoods GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,stockOrderCode,goodsId,goodsCount,modelTypeId,goodsStockPrice from tbStockOrderGoods ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            Gift.Model.tbStockOrderGoods model = new Gift.Model.tbStockOrderGoods();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stockOrderCode"] != null && ds.Tables[0].Rows[0]["stockOrderCode"].ToString() != "")
                {
                    model.stockOrderCode = ds.Tables[0].Rows[0]["stockOrderCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["goodsId"] != null && ds.Tables[0].Rows[0]["goodsId"].ToString() != "")
                {
                    model.goodsId = int.Parse(ds.Tables[0].Rows[0]["goodsId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["goodsCount"] != null && ds.Tables[0].Rows[0]["goodsCount"].ToString() != "")
                {
                    model.goodsCount = int.Parse(ds.Tables[0].Rows[0]["goodsCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["modelTypeId"] != null && ds.Tables[0].Rows[0]["modelTypeId"].ToString() != "")
                {
                    model.modelTypeId = int.Parse(ds.Tables[0].Rows[0]["modelTypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["goodsStockPrice"] != null && ds.Tables[0].Rows[0]["goodsStockPrice"].ToString() != "")
                {
                    model.goodsStockPrice = decimal.Parse(ds.Tables[0].Rows[0]["goodsStockPrice"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbStockOrderGoods GetModel(string sStockOrderCode,int iGoodsId,int modelTypeId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,stockOrderCode,goodsId,goodsCount,modelTypeId,goodsStockPrice,isnull(inWarehouseCount,0) as inWarehouseCount  from tbStockOrderGoods ");
            strSql.Append(" where stockOrderCode=@stockOrderCode and goodsId=@goodsId and modelTypeId=@modelTypeId");
            SqlParameter[] parameters = {
                    new SqlParameter("@stockOrderCode", SqlDbType.VarChar,50),
					new SqlParameter("@goodsId", SqlDbType.Int,4),
                    	new SqlParameter("@modelTypeId", SqlDbType.Int,4)
			};
            parameters[0].Value = sStockOrderCode;
            parameters[1].Value = iGoodsId;
            parameters[2].Value = modelTypeId;

            Gift.Model.tbStockOrderGoods model = new Gift.Model.tbStockOrderGoods();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stockOrderCode"] != null && ds.Tables[0].Rows[0]["stockOrderCode"].ToString() != "")
                {
                    model.stockOrderCode = ds.Tables[0].Rows[0]["stockOrderCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["goodsId"] != null && ds.Tables[0].Rows[0]["goodsId"].ToString() != "")
                {
                    model.goodsId = int.Parse(ds.Tables[0].Rows[0]["goodsId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["goodsCount"] != null && ds.Tables[0].Rows[0]["goodsCount"].ToString() != "")
                {
                    model.goodsCount = int.Parse(ds.Tables[0].Rows[0]["goodsCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["modelTypeId"] != null && ds.Tables[0].Rows[0]["modelTypeId"].ToString() != "")
                {
                    model.modelTypeId = int.Parse(ds.Tables[0].Rows[0]["modelTypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["goodsStockPrice"] != null && ds.Tables[0].Rows[0]["goodsStockPrice"].ToString() != "")
                {
                    model.goodsStockPrice = decimal.Parse(ds.Tables[0].Rows[0]["goodsStockPrice"].ToString());
                }
                model.InWarehouseCount = int.Parse(ds.Tables[0].Rows[0]["inWarehouseCount"].ToString());
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,stockOrderCode,goodsId,goodsCount,modelTypeId,goodsStockPrice ");
            strSql.Append(" FROM tbStockOrderGoods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListByStockOrderCode(string sStockOrderCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sog.Id,g.goodsCode,sog.stockOrderCode,g.goodsId,g.goodsName,sog.goodsCount,mt.modelTypeId,mt.modelTypeName,sog.goodsStockPrice, sog.goodsStockPrice * sog.goodsCount as sumStockPrice ");
            strSql.Append(" FROM tbStockOrderGoods sog join tbGoods g on sog.goodsId=g.goodsId left join tbModelType mt on g.goodsId=mt.goodsId and isnull(g.isModelType,0)=1 and sog.modelTypeId = mt.modelTypeId ");
            strSql.Append(" where sog.stockOrderCode = @stockOrderCode ");
            SqlParameter[] paramters = 
            {
                new SqlParameter("stockOrderCode",SqlDbType.VarChar,50)
            };
            paramters[0].Value = sStockOrderCode;

            return DbHelperSQL.Query(strSql.ToString(),paramters);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,stockOrderCode,goodsId,goodsCount,modelTypeId,goodsStockPrice ");
            strSql.Append(" FROM tbStockOrderGoods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbStockOrderGoods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from tbStockOrderGoods T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetGoodsListByOrderCode(string sOrderCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select g.goodsCode,g.goodsId,g.goodsName,og.goodsStockPrice,og.goodsCount,og.goodsStockPrice * og.goodsCount as sumOrderPrice,isnull(og.inWarehouseCount,0) as inWarehouseCount, ");
            strSql.Append("isnull((select sum(goodsCount) from  tbWarehouseGoodsOut where orderId in (select id from tbStockOrder where stockOrderCode = @stockOrderCode)),0) as outWarehouseCount ");
            strSql.Append(",  isnull(og.modelTypeId,0) as modelTypeId, isnull((select modelTypeName from tbModelType where modelTypeId= og.modelTypeId),'') as modelTypeName ");
            strSql.Append(" FROM tbStockOrderGoods og join tbGoods g on og.goodsId=g.goodsId ");
            strSql.Append(" where og.stockOrderCode = @stockOrderCode ");
            SqlParameter[] parameters = 
            {
              new SqlParameter("@stockOrderCode", SqlDbType.VarChar,50)
            };
            parameters[0].Value = sOrderCode;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbStockOrderGoods";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

